package com.cs171.market.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cs171.market.entity.Catogery;
import com.cs171.market.mapper.CategoryMapper;
import com.cs171.market.service.CategoryService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Endy
 * @create 2020 - 06 - 20 - 13:08
 * @date 2020/6/20 - 13:08
 */
@RestController
public class CategoryController {
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    CategoryService categoryService;
    @CrossOrigin
    @GetMapping("/categories")
    public Object allCategories(@RequestParam(value = "type", defaultValue = "3")Integer type,
                                @RequestParam(value = "pagenum", defaultValue = "0")Integer pagenum,
                                @RequestParam(value = "pagesize", defaultValue = "0")Integer pagesize)
    {
        //pageNum--总共的页数
        Integer success=200;
        JSONObject jsonObject = new JSONObject();
        JSONObject metaObject = new JSONObject();
        JSONArray cat_list = new JSONArray();
        if (pagenum==0 || pagesize==0)
        {
            cat_list = (JSONArray) JSON.toJSON(categoryService.queryCatList());
        }
        else
        {
            cat_list = (JSONArray) JSON.toJSON(categoryService.queryPageCatList(pagenum, pagesize));
        }
        metaObject.put("msg","获取成功");
        metaObject.put("status",success);
        jsonObject.put("data",cat_list);
        jsonObject.put("meta",metaObject);
        return jsonObject;
    }

    @PostMapping("/categories")
    public Object addCategory(@RequestParam("cat_pid")Integer cat_pid,
                              @RequestParam("cat_name") String cat_name, @RequestParam("cat_level")Integer cat_level)
    {
        Integer success=200;
        JSONObject jsonObject = new JSONObject();
        JSONObject metaObject = new JSONObject();
        categoryMapper.addCategory(cat_pid, cat_name, cat_level);
        Catogery catogery = categoryMapper.responseAdd();
        JSONObject list = new JSONObject();
        list.put("cat_id",catogery.getCat_id());
        list.put("cat_name",catogery.getCat_name());
        list.put("cat_pid",catogery.getCat_pid());
        list.put("cat_level",catogery.getCat_level());
        metaObject.put("msg","创建成功");
        metaObject.put("status",success);
        jsonObject.put("data",list);
        jsonObject.put("meta",metaObject);
        return jsonObject;
    }

    @GetMapping("/categories/{id}")
    public Object findCategoryById(@PathVariable("id")Integer id)
    {
        Integer success=200;
        JSONObject jsonObject = new JSONObject();
        JSONObject metaObject = new JSONObject();
        Catogery catogery = categoryMapper.findCategoryById(id);
        JSONObject list = new JSONObject();
        list.put("cat_id",catogery.getCat_id());
        list.put("cat_name",catogery.getCat_name());
        list.put("cat_pid",catogery.getCat_pid());
        list.put("cat_level",catogery.getCat_level());
        metaObject.put("msg","获取成功");
        metaObject.put("status",success);
        jsonObject.put("data",list);
        jsonObject.put("meta",metaObject);
        return jsonObject;
    }

    @PutMapping("/categories/{id}")
    public Object setCategory(@PathVariable("id")Integer id, @RequestParam("cat_name")String cat_name)
    {
        Integer success=200;
        JSONObject jsonObject = new JSONObject();
        JSONObject metaObject = new JSONObject();
        categoryMapper.setCategory(id, cat_name);
        Catogery catogery = categoryMapper.findCategoryById(id);
        JSONObject list = new JSONObject();
        list.put("cat_id",catogery.getCat_id());
        list.put("cat_name",catogery.getCat_name());
        list.put("cat_pid",catogery.getCat_pid());
        list.put("cat_level",catogery.getCat_level());
        metaObject.put("msg","更新成功");
        metaObject.put("status",success);
        jsonObject.put("data",list);
        jsonObject.put("meta",metaObject);
        return jsonObject;
    }

    @DeleteMapping("/categories/{id}")
    public Object deleteCategory(@PathVariable("id")Integer id)
    {
        Integer success=200;
        JSONObject jsonObject = new JSONObject();
        JSONObject metaObject = new JSONObject();
        categoryMapper.deleteCategory(id);
        metaObject.put("msg","删除成功");
        metaObject.put("status",success);
        jsonObject.put("data","null");
        jsonObject.put("meta",metaObject);
        return jsonObject;
    }
}
